package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hcrazy.manchester.app.pojo.Friend;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class FriendDAO extends BaseDAO<Friend>{

	public PageModel getFriends(long userId,int status,PageModel model) throws Exception{
		String sql = "select a.*,c.username friendname,d.username,e.header friendHeader,f.header userHeader from " +
				"him_user_friend a,him_user c,him_user d,him_user_info e,him_user_info f " +
				" where a.friendId = c.userId and a.userId = d.userId " +
				"and a.friendId = e.userId and a.userId = f.userId and c.userId = e.userId and d.userId = f.userId";
		
		StringBuilder querySQL = new StringBuilder("select * from ("+sql+") as m");
		StringBuilder countSQL = new StringBuilder("select count(*) from ("+sql+") as m");
		
		StringBuilder whereSQL = new StringBuilder(" where friendId = ? and status = ?");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(userId);
		paramList.add(status);
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Friend.class);
	}
	
	public PageModel getAllowedFriends(long userId,int status,PageModel model) throws Exception{
		
		String sql = "select a.userId,a.friendId,a.status,a.message,c.username friendname,d.username,e.header friendHeader,f.header userHeader from " +
				"him_user_friend a,him_user c,him_user d,him_user_info e,him_user_info f " +
				" where a.friendId = c.userId and a.userId = d.userId " +
				"and a.friendId = e.userId and a.userId = f.userId and a.userId= ? and a.status=? " +
				"union select a1.friendId userId,a1.userId friendId,a1.status,a1.message,c1.username friendname,d1.username,e1.header friendHeader,f1.header userHeader from " +
				"him_user_friend a1,him_user c1,him_user d1,him_user_info e1,him_user_info f1 " +
				" where a1.friendId = d1.userId and a1.userId = c1.userId " +
				"and a1.friendId = f1.userId and a1.userId = e1.userId and a1.friendId= ? and a1.status=? ";
		
		StringBuilder querySQL = new StringBuilder("select * from ("+sql+") as m");
		StringBuilder countSQL = new StringBuilder("select count(*) from ("+sql+") as m");
		
		//StringBuilder whereSQL = new StringBuilder(" where (userId = ? or friendId = ?) and status = ?");
		StringBuilder whereSQL = new StringBuilder("");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(userId);
		paramList.add(status);
		paramList.add(userId);
		paramList.add(status);
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Friend.class);
	}

	public String getFriendStatus(long userId, long friendId) {
		String sql = "select * from him_user_friend where userId=? and friendId=?";
		List<Friend> list = new ArrayList<Friend>();
		try {
			list = this.getList(sql, Friend.class, userId, friendId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list.isEmpty()){
			return "" + Constant.USER_FRIEND_IRRELEVANT;
		}else{
			return "" + list.get(0).getStatus();
		}
	}
}
